package cn.wx.scholar.core.service;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.wx.scholar.core.entity.TxtData;
import cn.wx.common.persistence.base.BaseService;
import cn.wx.scholar.core.mapper.TxtDataMapper;
import cn.wx.scholar.core.service.async.AsyncTxtDataFos;
import cn.wx.common.persistence.exception.MyException;
import cn.wx.common.utils.JSONUtils;


/**
 * <p>
 *  服务类
 * </p>
 *
 * @author  
 * @since 2018-07-06
 */
@Service
@Transactional
public class TxtDataService extends BaseService<TxtData, TxtDataMapper> {

	private final static String fDirectory = "D:\\my_mag";
	
	private final static String fPath = "D:\\my_mag\\part";
	private final static String hz = ".txt";
	
	private final static String fErrorPath = "D:\\my_mag_error.txt";
	
	private final static String tableName = "txt_data_1";
	
	//设读取文件的缓存为20MB 
	private final static int bufferSize = 20 * 1024 * 1024;
	
	@Autowired
	private AsyncTxtDataFos asyncTxtDataFos;
	
	public void insertByXml(TxtData t) {
		simpleMapper.insertBatch(t);
	}
	
	/**
	 * 获取最后一个插入的txt对应的表名
	 * @return
	 */
	public String getTxtDataLastTable() {
		return simpleMapper.getTxtDataLastTable();
	}
	

	
	/**
	 * 动态创建表
	 * @param map
	 */
	public void createTxtDataTable(String tableName) {
		int e = simpleMapper.exitTxtDataTable(tableName);
		if(e == 0) {
			simpleMapper.createTxtDataTable(tableName);
		}
		simpleMapper.updateTxtDataLastTable(tableName);
	}
	
	
	
	public void deleteByTxtFile(Map<String,Object> map) {
		simpleMapper.deleteByTxtFile(map);
	}
		
	
	
	/**
	 * 构建领域
	 * @param filePath
	 * @return
	 */
	
	
	public void distinctFos() {
		
		/**
		 * 对数据进行分片
		 * 开启多线程
		 */
		int pageNo = 0;
		while(true) {
			
			/**
			 * 分页条件组装
			 */
			Map<String,Integer> param = new HashMap<>();
			param.put("pageNo" , pageNo * 2000);
			param.put("pageSize" , 2000);
			
			List <String> page = simpleMapper.queryDistinctFos(param);
			
			if(page != null && page.size() > 0) {
				/**
				 * 开启多线程
				 */

				asyncTxtDataFos.txtDataFos(page);
				
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}else {
				/**
				 * 跳出while循环
				 */
				break;
			}
			
			pageNo++;
			
		}
		
	}
	
	
	 private BufferedReader initBr(final String filePath) {
		try {
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
	        //注意这里有时会乱码，根据自己的文本存储格式，进行调整
	        InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream,"utf-8");  
	        BufferedReader input = new BufferedReader(inputStreamReader, bufferSize);
	        return input;
		}catch (Exception e) {
		}
        return null;
	}
	
	 
	/**
	 * 查找一个文件下的所有文件 
	 */
	 public void fealFileTxtsError() {
		 File file = new File(fDirectory);
		 
		 /**
		  * 当这个file是文件夹
		  */
		 if(file.isDirectory()) {
			 String[] filelist = file.list();
			 
			 if(filelist.length > 0) {
				 for (String fl : filelist) {
	            	 final String index = fl.replace("part", "").replace(".txt", "");
	            	 fealOneTxtError(Integer.parseInt(index));
	             }
			 }
		 }
	 }

	 
	/**
	 * 查询一个txt的错误
	 */
	public void fealOneTxtError(int startIndex) {
		int i = 0;
		try {
			String filePath = fPath + (startIndex) + hz;
			BufferedReader input = initBr(filePath);
			
			if(input != null) {
				
				Map<String,Object> map = new HashMap<>();
				map.put("tableName", tableName);
				map.put("txtFile", startIndex);
				
				simpleMapper.deleteByTxtFile(map);
				
				String line;
				while((line = input.readLine()) != null) {
					try {
						if(StringUtils.isNotBlank(line)) {
							TxtData txtData = null;
							txtData = JSONUtils.p(line, TxtData.class);
							txtData.setTxtFile(startIndex);
							List<TxtData> list = txtData.newArrayTxtDatas();
							for(TxtData sr : list) {
								super.insert(sr);
							}
							i++;
						}
					}catch (Exception e) {
						
						/**
						 * 发生错误
						 * 将错误数据写入text
						 */
						BufferedWriter output = null;
						output = new BufferedWriter(
	                    		new OutputStreamWriter(
	                    				new FileOutputStream( 
	                    						new File(fErrorPath)),"utf-8"));
						output.append(line + "\r");
						output.flush();  
	                    output.close();  
	                    output = null;
						continue;
					}
					
				}
			}
			input.close();
			input = null;
			
			File file = new File(filePath);
			file.delete();
			
		} catch (Exception e) {
			throw new MyException("第" + i+ "行出现了错误，请查找" + e.getMessage());
		}
	}
}
